package com.hunktimes.wholesale.util

import android.content.Context
import android.os.Build
import android.util.DisplayMetrics
import android.util.TypedValue
import android.view.Window
import android.view.WindowManager

/**
 * Author: Jax
 * Date: 2019/11/20 16:55
 * Description:
 */
object DisplayUtil {
    fun getWidthAndHeight(window: Window): IntArray? {
        if (window == null) {
            return null
        }
        val integer = intArrayOf(-1,-1)
        val dm = DisplayMetrics()
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            window.windowManager.defaultDisplay.getRealMetrics(dm)
        } else {
            window.windowManager.defaultDisplay.getMetrics(dm)
        }
        integer[0] = dm.widthPixels
        integer[1] = dm.heightPixels
        return integer
    }

    fun getScreenWidth(context: Context): Int {
        val wm = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
        val displayMetricx = DisplayMetrics()
        wm.defaultDisplay.getMetrics(displayMetricx)
        return displayMetricx.widthPixels
    }

    fun getScreenHeight(context: Context): Int {
        val wm = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
        val displayMetricx = DisplayMetrics()
        wm.defaultDisplay.getMetrics(displayMetricx)
        return displayMetricx.heightPixels
    }

    @JvmStatic
    fun dp2px(context: Context, dp: Float): Int {
        val r = context.resources
        val px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, r.displayMetrics)
        return Math.round(px)
    }

    @JvmStatic
    fun sp2px(context:Context, dp:Float): Int{
        val r = context.resources
        val px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, dp, r.displayMetrics)
        return Math.round(px)
    }
}